//
//  CommentSeekVC.swift
//  ShareUmbrella
//
//  Created by duanmingyang on 2017/9/18.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit

class CommentSeekVC: YSBaseViewController,UITableViewDelegate,UITableViewDataSource{
    let kScreenHeight = UIScreen.main.bounds.size.height
    let kScreenWidth = UIScreen.main.bounds.size.width
    var selfTableView:UITableView!
    var emptyImage:UIImageView! = nil
    var dataArr = NSArray.init()
    var arr1 = NSMutableArray.init()//name
    var arr2 = NSMutableArray.init()//picture
    var arr3 = NSMutableArray.init()//star
    var arr4 = NSMutableArray.init()//time
    var arr5 = NSMutableArray.init()//comment
    
    override func setup() {
        super.setup()
    }
    
    /*
     *
     * 界面布局初始化
     */
    override func buildUI() {
        super.buildUI()
        self.title = I18n("VC.LFSide.seekcomment.titlte")
        
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        self.initBackgourndViewColor()
        self.addEmptyDataView()
        self.addSelfTableView()
        self.commentHistroyAction()
        
        
    }
    
    //
    override func buildRx() {
        super.buildRx()
    }
    
    //RGB
    func RGB(r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor{
        return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
    }
    
    //初始化背景颜色
    func initBackgourndViewColor() {
        self.view.backgroundColor = self.RGB(r: 246, 246, 246)
    }
    
    //数据为空时候的图片
    func addEmptyDataView(){
        emptyImage = UIImageView.init(frame: CGRect(x: (kScreenWidth-100)/2, y: kScreenHeight * 0.3, width: 100, height: 100))
        emptyImage.image = UIImage.init(named: "暂无数据")
        emptyImage.alpha = 0.0
        self.view.addSubview(emptyImage)
    }
    
    //添加一个tableview
    func addSelfTableView(){
        selfTableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-64), style: UITableViewStyle.plain)
        selfTableView.dataSource = self
        selfTableView.delegate = self
        selfTableView.backgroundColor = UIColor.clear
        selfTableView.separatorStyle = UITableViewCellSeparatorStyle.none;
        selfTableView.showsHorizontalScrollIndicator = false
        selfTableView.showsVerticalScrollIndicator = false
        self.view.addSubview(selfTableView)
    }
    
    //用户信息
    func commentHistroyAction(){
        HUD.show()
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.commentHistroyAction(token: Auth.token,type:CommentHistroyType.seek,pageIndex:"1",pageCount:"20"))).subscribe(onNext: { [weak self] (data) in
            //
            HUD.hide()
            
            guard let `self` = self, let `data` = data else {
                return
            }
            
            self.dataArr = data["data"].arrayValue as NSArray
            
            if(self.dataArr.count<=0){
                self.emptyImage.alpha = 1.0
                return
            }else{
                self.emptyImage.alpha = 0.0
            }
            
            for index in 0...self.dataArr.count-1 {
                self.arr1.add(data["data"][index]["name"].stringValue)
                self.arr2.add(data["data"][index]["picture"].stringValue)
                self.arr3.add(data["data"][index]["star"].stringValue)
                self.arr4.add(data["data"][index]["time"].stringValue)
                self.arr5.add(data["data"][index]["comment"].stringValue)
            }
            
            
            self.selfTableView.reloadData()
            }, onError: { [weak self](ErrorType) in
                HUD.hide()
                guard let  `self` = self else {
                    return
                }
                self.handle(withError: ErrorType)
        })
        
    }
    
    //MARK:UITableView DataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let iderntify:String = "swiftCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: iderntify) as? CommentCell
        if(cell == nil){
            cell=CommentCell(style: UITableViewCellStyle.default
                , reuseIdentifier: iderntify);
            
        }
        let section = indexPath.section
        
        let name = arr1[section] as? String ?? ""
        let picture = arr2[section] as? String ?? ""
        let star = arr3[section] as? String ?? ""
        let time = arr4[section] as? String ?? ""
        let comment = arr5[section] as? String ?? ""
        
        cell?.setData(name: name, picture: picture, star: star, time: time, comment: comment)
        
        return cell!
    }
    
    //MARK:UITableView Delegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selfTableView?.deselectRow(at: indexPath, animated: true)
        print("你选中了\(indexPath)" )
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 130
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(section == 0){
            return 25
        }
        return 20
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 20))
        if(section == 0){
            view.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 25)
            view.backgroundColor = RGB(r: 246, 246, 246)
            let l = UILabel.init(frame: CGRect(x: 15, y: 0, width: kScreenWidth, height: 25))
            l.text = "我的求助消息记录"
            l.font = UIFont.systemFont(ofSize: 13)
            l.textColor = RGB(r: 155, 155, 155)
            view.addSubview(l)
        }
        return view
    }
}
